﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#if WINDOWS_PHONE
using Microsoft.Xna.Framework.Input.Touch;
#endif
#if WINDOWS
using Microsoft.Xna.Framework.Storage;
#endif
using Microsoft.Xna.Framework.Media;
using Neat;
using Neat.MenuSystem;
using Neat.EasyMenus;
using Neat.GUI;
using Neat.Mathematics;
using Neat.Graphics;
using System.IO;
namespace RiverRaidX
{
    public partial class RiverRaidGame : Screen
    {
        public void Reset()
        {
            InitForm();

            engineInstance = game.GetSound("engine").CreateInstance();
            engineInstance.IsLooped = true;
            engineStartInstance = game.GetSound("engine_start").CreateInstance();
            engineStartInstance.IsLooped = false;
            fuelInstance = game.GetSound("fuel").CreateInstance();
            fuelInstance.IsLooped = true;
            staticInstance = game.GetSound("static").CreateInstance();
            staticInstance.IsLooped = true;

            yOffset = 0;
            score = 0;
            fuel = maxFuel;
            lives = initLives;
            pxVel = 0f;
            state = GameStates.StartAnimation;// -1;
            ar = new Vector2((float)game.GameWidth / originalGameSize.X,
                (float)game.GameHeight / originalGameSize.Y);
            scale = new Vector2(0.4f) * ar;

            rocketSize = new Vector2(5, 20) * ar;
            fuelUseRate = 0.75f * ar.Y;
            xForce = 170f * ar.X;
            ySpeed = 6f * ar.Y;
            rocketSpeed = 40f * ar.Y;

            var playerTexture = game.GetTexture("raider");
            var heliTexture = game.GetTexture("heli");
            var fuelTexture = game.GetTexture("fuel");
            var shipTexture = game.GetTexture("ship");
            var jetTexture = game.GetTexture("jet");
            var bridgeTexture = game.GetTexture("bridge");

            stillPlayerWidth = playerTexture.Width * scale.X;
            movingPlayerWidth = game.GetTexture("raider_mv").Width * scale.X;
            playerMesh = Polygon.BuildRectangle(
                (game.GameWidth - playerTexture.Width * scale.X) / 2f,
                (game.GameHeight - 100),
                playerTexture.Width * scale.X,
                playerTexture.Height * scale.Y);

            grassColors = new List<Color>();
            grassColors.Add(Color.ForestGreen);
            grassColors.Add(Color.DarkGreen);
            grassColor = grassColors[0];

            outerWalls = new List<Vector2>();
            innerWalls = new List<Vector2>();
            entities = new List<Entity>();
            deadEntities = new List<Entity>();
            checkpoints = new List<float>();

            siren.X = 0.0f;

            levelPaths = new List<string>();
            var f = Directory.GetFiles(".\\", "*.rrx");

            foreach (var item in f)
                levelPaths.Add(item);

            LoadLevel(levelPaths[lastLoadedLevel = 0]);
            currentLevel = startingLevel = lastLoadedLevel;

            CreateRects();
            yOffset -= game.GameHeight;

            game.Console.AddCommand("rr_fuel", rr_fuel);
            game.Console.AddCommand("rr_noplayer", rr_noplayer);
            game.Console.AddCommand("rr_postfx", rr_postfx);
            game.Console.AddCommand("rr_map", rr_map);
            game.Console.AddCommand("rr_lives", rr_lives);
            game.Console.AddCommand("rr_shoot", rr_shoot);
            //game.Console.Run("dv rr_effects " + (!NoPostFx));
            game.Console.Run("e_playsongs");
        }

        void Die()
        {
            if (!NoPlayer)
            {
                shotInARow = 0;
                engineInstance.Stop();
                fuelInstance.Stop();
                staticInstance.Stop();
                state = GameStates.DeathAnimation;// 1;
                deathFrame = deathTime;
                game.PlaySound("explosion");
            }
        }

        void Reborn()
        {
            lives--;
            if (lives < 0)
            {
                //Game Over
                game.Console.Run("dv score " + score);
                game.Console.Run("sh gameoverscreen");
                level = 0;
                return;
            }
            
            state = GameStates.StartAnimation;// -1;
            yOffset = 0;
            fuel = maxFuel;
            pxVel = 0f;
            var playerTexture = game.GetTexture("raider");
            playerMesh = Polygon.BuildRectangle(
                (game.GameWidth - playerTexture.Width * scale.X) / 2f,
                (game.GameHeight - 100),
                playerTexture.Width * scale.X,
                playerTexture.Height * scale.Y);
            outerWalls = new List<Vector2>();
            innerWalls = new List<Vector2>();
            entities = new List<Entity>();
            checkpoints = new List<float>();
            startingLevel = currentLevel;
            if (currentLevel >= levelPaths.Count) currentLevel %= levelPaths.Count;
            LoadLevel(levelPaths[currentLevel]);
            lastLoadedLevel = currentLevel;
            CreateRects();
            yOffset -= game.GameHeight;
            rocketLaunched = false;
        }

        void rr_fuel(IList<string> args)
        {
            fuel = int.Parse(args[1]);
        }

        void rr_noplayer(IList<string> args)
        {
            NoPlayer = bool.Parse(args[1]);
        }

        void rr_postfx(IList<string> args)
        {
            NoPostFx = ! bool.Parse(args[1]);
        }

        void rr_map(IList<string> args)
        {
            string fname = game.Console.Args2Str(args, 1);
            if (!File.Exists(fname) && fname != "random")
            {
                game.Console.WriteLine("File " + fname + " does not exist.");
                return;
            }
            levelPaths.Clear();
            levelPaths.Add(fname);
        }

        void rr_lives(IList<string> args)
        {
            if (args.Count == 1) game.Console.WriteLine(lives.ToString());
            else lives = int.Parse(args[1]);
        }

        void rr_shoot(IList<string> args)
        {
            if (state == GameStates.Game)
            {
                if (args[1] == "shoot" || args[1] == "bang")
                {
                    //Messages.Add(GameMessages.ShootRocket);
                    //game.Console.Run("et_echo " + args[1]);
                }
                //Messages.Add(GameMessages.ShootRocket);
            }
        }
    }
}
